package com.zareal.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.zareal.domain.Storage;
import com.zareal.dot.GetProductDto;
import com.zareal.query.StorageQuery;
import com.zareal.result.JSONResult;
import com.zareal.result.PageList;
import com.zareal.service.IStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/storage")
public class StorageController {

    @Autowired
    public IStorageService storageService;

    // 入库
    @RequestMapping(value = "/putstorage", method = RequestMethod.POST)
    public JSONResult putStorage(@RequestBody GetProductDto getProductDto) {
        JSONResult jsonResult = storageService.putStorage(getProductDto);
        return JSONResult.success(jsonResult);
    }

    ;

    // 出库
    @RequestMapping(value = "/outstorage", method = RequestMethod.POST)
    public JSONResult outStorage(@RequestBody GetProductDto getProductDto) {
        JSONResult jsonResult = storageService.outStorage(getProductDto);
        if (jsonResult.getData() == null) {
            return JSONResult.error("出库失败");
        }
            return JSONResult.success(jsonResult.getData());
    }
    ;

    /**
     * 查询所有缺货的
     */
    @RequestMapping(value = "/outofstock", method = RequestMethod.GET)
    public JSONResult outOfStock() {
        JSONResult jsonResult = storageService.outOfStock();
        return JSONResult.success(jsonResult.getData());
    }

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Storage storage) {
        if (storage.getId() != null) {
            storageService.updateById(storage);
        } else {
            storageService.insert(storage);
        }
        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        storageService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(storageService.selectById(id).getNumber());
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(storageService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody StorageQuery query) {
        Page<Storage> page = new Page<Storage>(query.getPage(), query.getRows());
        page = storageService.selectPage(page);
        return JSONResult.success(new PageList<Storage>(page.getTotal(), page.getRecords()));
    }
}
